package com.briup.jd2407cms.web.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.briup.jd2407cms.domain.User;
import com.briup.jd2407cms.domain.extend.UserExtend;
import com.briup.jd2407cms.result.Result;
import com.briup.jd2407cms.service.UserService;
import com.briup.jd2407cms.utils.MD5Utils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @BelongsProject: cms2
 * @BelongsPackage: com.briup.jd2407cms.web.controller
 * @Author: wangjie
 * @CreateTime: 2024-11-20  09:52
 * @Description: TODO
 * @Version: 1.0
 */
@Api(tags = "用户模块")
@RequestMapping("/auth/user")
@RestController
@Slf4j
public class UserController {
    @Autowired
    UserService userService;
    @ApiOperation("添加用户")
    @PostMapping("/save")
    private Result save(@RequestBody User user){
        user.setPassword(MD5Utils.MD5(user.getPassword()));
        userService.add(user);
        return Result.success();
    }
    @ApiOperation("获取登陆用户信息")
    @GetMapping("/info")
    private Result getInfo(@RequestAttribute("userId") Long userId){

        log.info("id{}",userId);
        return Result.success(userService.queryById(userId));
    }
    @ApiOperation("分页条件查询")
    @GetMapping("/query")
    private Result query(@RequestParam Integer pageNum,@RequestParam Integer pageSize,
                         String username,String status,Integer roleId,Integer isVip){

        IPage<UserExtend> users = userService.query(pageNum, pageSize, username, status, roleId, isVip);
        return Result.success(users);
    }
    @ApiOperation("根据id查询用户")
    @GetMapping("/queryById/{id}")
    private Result queryById(@PathVariable Long id){
        User user = userService.queryById(id);
        return Result.success(user);
    }
    @ApiOperation("给指定用户充vip")
    @PutMapping("/setVip/{id}")
    private Result setVip(@PathVariable Long id){
        userService.setVip(id);
        return Result.success();
    }
    @ApiOperation("更新用户数据")
    @PutMapping("/update")
    private Result update(@RequestBody User user){
        userService.updateById(user);
        return Result.success();
    }
    @ApiOperation("删除用户")
    @DeleteMapping("/deleteByBatch/{ids}")
    private Result deleteByBatch(@PathVariable List<Long> ids){
        userService.deleteByBatch(ids);
        return Result.success();
    }
    @ApiOperation("查询所有用户")
    @GetMapping("/getAllUser")
    private Result getAllUser(){
        List<User> allUser = userService.getAllUser();
        return Result.success(allUser);
    }
}

